package com.tianyang.yukuaifuzs.business.main.presenter;

import android.content.Intent;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifuzs.business.login.bean.LoginOutput;
import com.tianyang.yukuaifuzs.business.main.bean.ChannelOutput;
import com.tianyang.yukuaifuzs.business.main.bean.UserStoreOutput;
import com.tianyang.yukuaifuzs.business.main.contract.MainConfig;
import com.tianyang.yukuaifuzs.business.main.contract.StoreManageContract;
import com.tianyang.yukuaifuzs.business.main.model.MainModel;
import com.tianyang.yukuaifuzs.business.main.view.MainActivity;
import com.tianyang.yukuaifuzs.common.cache.CacheManage;
import com.tianyang.yukuaifuzs.common.cache.CacheModel;
import com.tianyang.yukuaifuzs.utils.IntentTool;
import com.tianyang.yukuaifuzs.utils.ToastUtil;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：店铺门店管理
 * Created by xyj on 2024/3/11
 */
public class StoreManagePresenter implements StoreManageContract.Presenter {

    private StoreManageContract.View mView;

    private CompositeSubscription mSubscription;

    public StoreManagePresenter(StoreManageContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getChannelList() {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().getChannelList()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ChannelOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<ChannelOutput> outputs) {
                        if (outputs != null && !outputs.isEmpty()) {
                            mView.showChannelView(outputs);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void getStoreManageList() {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().userMerchantStoresList()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<UserStoreOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<UserStoreOutput> outputs) {
                        mView.showStoreManageListView(outputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void getAnotherStoresToken(int memberId) {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().getAnotherStoresToken(memberId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(LoginOutput output) {
                        if (output != null) {
                            CacheManage.getInstance().saveCache(CacheModel.LOGIN_KEY, output);
                            CacheManage.getInstance().saveCache(CacheModel.IDENTITY, output.getIdentity());
                            Intent intent = new Intent(mView.getContext(), MainActivity.class);
                            intent.putExtra(MainConfig.INTENT_IS_CHANGE, true);
                            IntentTool.startActivity(mView.getContext(), intent, true);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void createNewStoreMerchant(String merchantType, String posChannel) {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().createNewStoreMerchant(merchantType, posChannel)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(JSONObject output) {
                        ToastUtil.show("店铺新增成功");
                        getStoreManageList();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
